package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import com.two.screen.water.balance.R
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.ToastUtil
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.DialogUtil


class ChooseDateDialog(context: Context) : Dialog(context, R.style.ChooseDialog),
    CalendarView.OnCalendarRangeSelectListener, CalendarView.OnMonthChangeListener,
    CalendarView.OnCalendarInterceptListener {
    @BindView(R.id.calendarView) lateinit var mCalendarView: CalendarView
    @BindView(R.id.mLastMonthTv) lateinit var mLastMonthTv: TextView
    @BindView(R.id.mNextMonthTv) lateinit var mNextMonthTv: TextView
    @BindView(R.id.mYearMonthTv) lateinit var mYearMonthTv: TextView
    @BindView(R.id.dialog_close_iv) lateinit var mCloseIv: ImageView

    private var mOnChooseDateDialogListener: OnChooseDateDialogListener? = null
    private val WEEK = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")

    private var mStartDateStr = ""
    private var mEndDateStr = ""

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_choose_data)
        this.setCanceledOnTouchOutside(true)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }
        ButterKnife.bind(this)


//        findViewById<View>(R.id.dialog_root).setOnClickListener {
//            dismiss()
//        }
        mCalendarView.setSelectRange(0, -1)
//        try {
//            var startList = CommUtils.toYMD(System.currentTimeMillis() / 1000).split("-")
//            var endList = CommUtils.toYMD(System.currentTimeMillis() / 1000 - 7 * 24 * 3600).split("-")
//            mCalendarView.setSelectCalendarRange(
//                Calendar().apply {
//                    year = startList[0].toInt()
//                    month = startList[1].toInt()
//                    day = startList[2].toInt()
//                },
//                Calendar().apply {
//                    year = endList[0].toInt()
//                    month = endList[1].toInt()
//                    day = endList[2].toInt()
//                }
//            )
//        } catch (e: Exception) {
//            CommUtils.println("转错了：", e.toString())
//        }
        mCalendarView.setOnCalendarRangeSelectListener(this)
        mCalendarView.setOnMonthChangeListener(this)
        //设置日期拦截事件，当前有效
        mCalendarView.setOnCalendarInterceptListener(this)
        //mCalendarView.setRange(2020, 1, 1, mCalendarView.curYear, mCalendarView.curMonth, mCalendarView.curDay)
        mCalendarView.post { mCalendarView.scrollToCurrent() }

        setYearMonth(mCalendarView.curYear, mCalendarView.curMonth)
        mYearMonthTv.setOnClickListener {
            mCalendarView.showYearSelectLayout(mCalendarView.curYear)
        }
        mCloseIv.setOnClickListener { dismiss() }
        mNextMonthTv.setOnClickListener { mCalendarView.scrollToNext(true) }
        mLastMonthTv.setOnClickListener { mCalendarView.scrollToPre(true) }
    }

    private fun setYearMonth(y: Int, m: Int) {
        mYearMonthTv.text = y.toString().plus("年").plus(m).plus("月")
    }

    fun setOnChooseDateDialogListener(l: OnChooseDateDialogListener) {
        mOnChooseDateDialogListener = l
    }

    interface OnChooseDateDialogListener {
        fun hadSelectedCalendar(mStartDateStr: String, mEndDateStr: String)
    }

    // TODO: 2018/9/13 超出范围提示
    override fun onCalendarSelectOutOfRange(calendar: Calendar?) {}

    @SuppressLint("SetTextI18n")
    override fun onCalendarRangeSelect(calendar: Calendar, isEnd: Boolean) {
        CommUtils.println("onCalendarRangeSelect:", "isEnd:$isEnd")
        if (!isEnd) {
            mStartDateStr = calendar.year.toString() + "-" + calendar.month + "-" + calendar.day
        } else {
            //TODO:这里判断。
            mEndDateStr = calendar.year.toString() + "-" + calendar.month + "-" + calendar.day
            mOnChooseDateDialogListener?.hadSelectedCalendar(mStartDateStr, mEndDateStr)
            dismiss()
        }
    }

    override fun onSelectOutOfRange(calendar: Calendar?, isOutOfMinRange: Boolean) {
        //ToastUtil.showToast(calendar.toString() + if (isOutOfMinRange) "小于最小选择范围" else "超过最大选择范围")
        ToastUtil.showToast("最大查询周期为1周！")
    }

    override fun onMonthChange(year: Int, month: Int) {
        setYearMonth(year, month)
    }

    override fun onCalendarInterceptClick(calendar: Calendar?, isClick: Boolean) {
        //ToastUtil.showToast(calendar.toString() + if (isClick) "拦截不可点击" else "拦截设定为无效日期")
        ToastUtil.showToast("超出当天了!")
    }

    override fun onCalendarIntercept(calendar: Calendar?): Boolean {
        val now = System.currentTimeMillis() + 24 * 3600 * 1000
        val today = calendar?.timeInMillis
        return now <= today ?: 0
    }
    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
